import { is_empty, uJob, show_mt, obj_str, uDb, reload_col, add_ck_col, ck_empty, loc, d_sort, cline } from 'sui';
import { show_val } from 'buy/task/cmd';

const get_ck = (cur, main, col, set) => {
    if (!set) {
        return null;
    }

    const ret = {
        sn : v => null,
        tp: v => ck_empty(v),
    };

    const tmp = col.filter(d => !set.has(d.sn));

    add_ck_col(ret, tmp);

    return ret;
};

const fix_col = () => {
    return [
        {sn: "sn", name: loc("l_488"), tp: "string", ctp: "auto"},
        {sn: "tp", name: loc("l_229"), tp: "string", ctp: "list"},
    ];
};

const load_col = (setCol) => {
    const fix = fix_col();
    reload_col(null, v => setCol(v), "partask", fix);
};

const load_custom = (setMain, mapSupply) => {
    uDb(null, "file/list/oiltp", { line: cline() },
        r => {
            if (setMain) {
                d_sort(r, "sn");
                setMain(r);
            }

            const map = {};
            r.forEach(d => map[d.sn] = d);

            mapSupply(map);
        },
        e => {
            if (setMain) {
                setMain(null);
            }

            mapSupply(null);
        });
};

const get_col = (tmp, col, map_supply, map_custom) => {
    col.forEach(d => {
        if (d.sn === "supply") {
            tmp.push({name: d.name, key: d.sn, width: 200, formatter: (props) => show_val(props, "supply", map_supply)});
        } else if (d.sn === "unit") {
            tmp.push({name: d.name, key: d.sn, width: 70, formatter: (props) => show_val(props, "custom", map_custom, "unit")});
        } else if (d.sn === "tp") {
            return;
        } else {
            show_mt(tmp, d, 160);
        }
    });
};

const get_custom = (map_custom, sn, k) => {
    if (!map_custom || !sn || !k) {
        return "";
    }

    const tmp = map_custom[sn];
    if (!tmp) {
        return "";
    }

    return tmp[k];
};

const load_part = (fnDone, cur, setMain) => {
    const tp = obj_str(cur, "sn");
    if (!tp) {
        setMain(null);
        return;
    }

    uJob(fnDone, "part/load",
        {
            line: cline(),
            tech: tp,
        },
        r => {
            r.forEach((d, i) => d["index"] = i + 1);
            setMain(r);
        }, e => setMain(null));
};

const load_kind = (setMain) => {
    uDb(null, "field/list/kind",
        {
            line: cline(),
        },
        r => {
            const set = new Set();

            set.add("name");

            r.forEach(d => {
                const sn = obj_str(d, "sn");
                if (sn === "sn") {
                    return;
                }

                set.add(sn);
            });

            set.add("code");
            set.add("start");

            setMain(set);
        }, e => setMain(null));
};

const load_tp = (cur, setMain) => {
    const user = cur.user;
    if (is_empty(user)) {
        setMain(null);
        return;
    }

    const set = new Set();
    user.forEach(d => set.add(d));

    uDb(null, "file/list/kind",
        {
            line: cline(),
        },
        r => {
            const ret = [];

            r.forEach(d => {
                const sn = obj_str(d, "sn");
                if (set.has(sn)) {
                    d["name"] = `${obj_str(d, "name")} ( ${obj_str(d, "std")} ${obj_str(d, "size")} )`;
                    ret.push(d);
                }
            });

            setMain(ret);
        }, e => setMain(null));
};

export {load_tp, load_kind, get_custom, get_col, load_part, load_custom, load_col, get_ck};
